package com.usian.user.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserRealnameService;
import com.usian.user.service.ApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser>implements ApUserService {

    @Autowired
    private ApUserRealnameService apUserRealnameService;
    @Autowired
    private WmUserFeign wmUserFeign;
    @Autowired
    private ApAuthorFeign apAuthorFeign;

    //用户审核业务
    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getStatus()!=2&&dto.getStatus()!=9){
            return ResponseResult.errorResult(AppHttpCodeEnum.APUSER_PARAM_INVALID);
        }
            //status 2失败   9通过
        ApUserRealname  apUserRealname=new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        if (dto.getStatus()==2&&dto.getMsg()!=null){
            apUserRealname.setReason(dto.getMsg());
        }
       apUserRealnameService.updateById(apUserRealname);


        //获取用户对象
        ApUserRealname userRealname=apUserRealnameService.getById(dto.getId());
        ApUser apUser=this.getById(userRealname.getUserId());
        if (apUser==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.1远程保存wm_user 查自媒体账号如果不存在 不存在就保存

        if (dto.getStatus()==9) {


            WmUser wmUser = wmUserFeign.findByNmae(apUser.getName());
            if (wmUser == null) {
                wmUser= new WmUser();                //设置ApUserId
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setStatus(9);
                wmUser.setPhone(apUser.getPhone());
                wmUserFeign.saveWmUser(wmUser);
            }
            //3保存作者信息
            ApAuthor apAuthor=apAuthorFeign.findByUserId(apUser.getId());
            if (apAuthor==null){
                apAuthor=new ApAuthor();
                apAuthor.setName(wmUser.getName());
                apAuthor.setType(2);
                apAuthor.setCreatedTime(new Date());
                apAuthor.setUserId(apUser.getId());
                apAuthor.setWmUserId(wmUser.getId());
                apAuthorFeign.saveApAuthor(apAuthor);
            }


        }

        apUser.setFlag((short)1);
        this.updateById(apUser);
//        int a=1/0;
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
